package com.wsc.springbootmultidevdemo.msg.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.wsc.springbootmultidevdemo.msg.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * confirm 串行消息发送
 */
public class Send {
    private static final String QUEUE_NAME = "confirm_model_queue";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        //设置成 confirm 模式 注意：同一个队列只能设置一种模式，如设置了 transaction 模式后，这个队列就不能再设置成 confirm 模式
        channel.confirmSelect();

        String msg = "confirm model test";

        channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());

        if(!channel.waitForConfirms()){
            System.out.println("message sent failed");
        }else {
            System.out.println("message sent success");
        }
        channel.close();
        connection.close();

    }
}
